﻿Imports MyIPBssLogic.AppConfig
Imports System.IO

Public Class frmMain

    Private Conf As Configuration
    Private Shared FileConf As String = Path.Combine( _
                                        My.Application.Info.DirectoryPath, _
                                          "Conf.xml")
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Conf.MyIPMailConf.From.User = txtAccountSender.Text
        Conf.MyIPMailConf.From.DisplayName = txtDisplay.Text
        Long.TryParse(txtInterval.Text, Conf.MyIPAppConfigData.TimeToCheckChange)
        Conf.MyIPMailConf.To.MailBox = txtMailReceipt.Text
        Conf.MyIPMailConf.From.MailBox = txtMailSender.Text
        Conf.MyIPMailConf.From.Password = txtPassword.Text
        Integer.TryParse(txtPort.Text, Conf.MyIPMailConf.From.Port)
        Conf.MyIPMailConf.From.NameSever = txtServerSender.Text
        Conf.MyIPMailConf.Email.Subject = txtSubject.Text
        Conf.MyIPAppConfigData.URLWebServLocateIP = txtWebSrv.Text
        Conf.MyIPMailConf.From.Secure = chkAut.Checked
        Conf.MyIPAppConfigData.SendMailAlwaysOnStart = chkSendOnStart.Checked

        If Not ConfMngmnt.Save(FileConf, Conf) Then
            MsgBox("Problems saving configuration")
            Exit Sub
        End If

        Try

            Try

                ToolStripStatusLabel1.Text = "Sttopping the service"
                ServiceController1.Stop()

            Catch ex As Exception

                ToolStripStatusLabel1.Text = "Service not started!!! starting...."

            End Try

            ToolStripStatusLabel1.Text = "Starting the service"
            ServiceController1.Start()

            ToolStripStatusLabel1.Text = "Done!!!"

            Beep()

            Me.Close()

        Catch ex As Exception

            ToolStripStatusLabel1.Text = "Oopss!! problems!!!"
            MsgBox(ex.ToString)


        End Try

    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Conf = ConfMngmnt.Load(FileConf)

        txtAccountSender.Text = Conf.MyIPMailConf.From.User
        txtDisplay.Text = Conf.MyIPMailConf.From.DisplayName
        txtInterval.Text = Conf.MyIPAppConfigData.TimeToCheckChange.ToString
        txtMailReceipt.Text = Conf.MyIPMailConf.To.MailBox
        txtMailSender.Text = Conf.MyIPMailConf.From.MailBox
        txtPassword.Text = Conf.MyIPMailConf.From.Password
        txtServerSender.Text = Conf.MyIPMailConf.From.NameSever
        txtSubject.Text = Conf.MyIPMailConf.Email.Subject
        txtWebSrv.Text = Conf.MyIPAppConfigData.URLWebServLocateIP
        txtPort.Text = Conf.MyIPMailConf.From.Port.ToString
        chkAut.Checked = Conf.MyIPMailConf.From.Secure
        chkSendOnStart.Checked = Conf.MyIPAppConfigData.SendMailAlwaysOnStart


    End Sub

    Private Sub txtInterval_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInterval.KeyPress

        If Not Char.IsDigit(e.KeyChar) Then
            Beep()
            e.Handled = True
        End If

    End Sub

    Private Sub txtPort_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPort.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then
            Beep()
            e.Handled = True
        End If
    End Sub

End Class
